package com.samsung.eci.wiki.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.samsung.eci.wiki.bean.Nodes;
import com.samsung.eci.wiki.dao.CommonMapper;
import com.samsung.eci.wiki.service.CommonService;

@Service
public class CommonServiceImpl implements CommonService {

	@Autowired
	private CommonMapper commonMapper;
	
	@Override
	public List<Nodes> getTree(String wikiId) {
		List<Nodes> nodes = commonMapper.getTree(0);
		if(nodes != null && nodes.size() > 0){
			for(Nodes node:nodes){
				List<Nodes> childs = commonMapper.getTree(node.getId());
				if(childs != null && childs.size()> 0){
					getChilds(node,childs);
				}
			}
		}
		return nodes;
	}

	private void getChilds(Nodes node, List<Nodes> childs) {
		// TODO Auto-generated method stub
		node.setNodes(childs);
		if(childs != null && childs.size() > 0){
			for(Nodes chind:childs){
				List<Nodes> childNodes = commonMapper.getTree(chind.getId());
				if(childNodes != null && childNodes.size()> 0){
					getChilds(chind,childNodes);
				}
			}
		}
	}

	

}
